import { UserStatus, ListUserRequest } from '@/api/user'
import { mockUsers } from '@/mock/data/userData'

// mock API 函数
export const mockAPI = {
  getUserList: async (params: ListUserRequest) => {
    // 列表查询延迟 1 秒
    await new Promise(resolve => setTimeout(resolve, 1000))
    let filteredUsers = [...mockUsers]
    
    // 按会议ID过滤
    if (params.meetId) {
      filteredUsers = filteredUsers.filter(user => user.meetId.toString() === params.meetId.toString())
    }
    
    // 按状态过滤
    if (params.status && params.status !== UserStatus.ALL) {
      filteredUsers = filteredUsers.filter(user => user.status === params.status)
    }
    
    // 计算分页
    const start = (params.page - 1) * params.size
    const end = start + params.size
    const currentData = filteredUsers.slice(start, end)
    
    return {
      data: {
        total_record: filteredUsers.length,
        total_page: Math.ceil(filteredUsers.length / params.size),
        current_data: currentData
      }
    }
  },

  approveUser: async (params: { phone: string; meetId: number }) => {
    // 审批操作延迟 1.2 秒
    await new Promise(resolve => setTimeout(resolve, 1200))
    const user = mockUsers.find(u => u.phone === params.phone && u.meetId === params.meetId)
    if (!user) throw new Error('User not found')
    user.status = UserStatus.APPROVED
    user.updateTime = new Date().toISOString()
    return { data: user }
  },

  batchApproveUsers: async (params: { records: Array<{ phone: string; meetId: number }> }) => {
    // 批量审批延迟 1.5 秒
    await new Promise(resolve => setTimeout(resolve, 1500))
    const updatedUsers = params.records.map(record => {
      const user = mockUsers.find(u => u.phone === record.phone && u.meetId === record.meetId)
      if (user) {
        user.status = UserStatus.APPROVED
        user.updateTime = new Date().toISOString()
      }
      return user
    }).filter(Boolean)
    return { data: updatedUsers }
  },

  rejectUser: async (params: { phone: string; meetId: number }) => {
    // 拒绝操作延迟 1.2 秒
    await new Promise(resolve => setTimeout(resolve, 1200))
    const user = mockUsers.find(u => u.phone === params.phone && u.meetId === params.meetId)
    if (!user) throw new Error('User not found')
    user.status = UserStatus.UNSIGNED
    user.updateTime = new Date().toISOString()
    return { data: user }
  },

  batchRejectUsers: async (params: { records: Array<{ phone: string; meetId: number }> }) => {
    // 批量拒绝延迟 1.5 秒
    await new Promise(resolve => setTimeout(resolve, 1500))
    const updatedUsers = params.records.map(record => {
      const user = mockUsers.find(u => u.phone === record.phone && u.meetId === record.meetId)
      if (user) {
        user.status = UserStatus.UNSIGNED
        user.updateTime = new Date().toISOString()
      }
      return user
    }).filter(Boolean)
    return { data: updatedUsers }
  },

  signInUser: async (params: { phone: string; meetId: number }) => {
    // 签到操作延迟 1 秒
    await new Promise(resolve => setTimeout(resolve, 1000))
    const user = mockUsers.find(u => u.phone === params.phone && u.meetId === params.meetId)
    if (!user) throw new Error('User not found')
    user.status = UserStatus.SIGNED
    user.updateTime = new Date().toISOString()
    return { data: user }
  },

  exportUserData: async (ids: number[]) => {
    // 导出操作延迟 2 秒
    await new Promise(resolve => setTimeout(resolve, 2000))
    const selectedUsers = mockUsers.filter(user => ids.includes(user.id))
    // 这里模拟返回一个空的 Blob 对象
    return { data: new Blob() }
  }
} 